package com.ruoyi.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.system.domain.HmzProduct;
import com.ruoyi.system.domain.HmzStorageRequestDetail;
import com.ruoyi.system.service.IHmzProductService;
import com.ruoyi.system.service.IHmzStorageRequestDetailService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.HmzStorageRequest;
import com.ruoyi.system.service.IHmzStorageRequestService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 用于存储入库申请信息的Controller
 *
 * @author ruoyi
 * @date 2023-11-10
 */
@RestController
@RequestMapping("/system/storagerequest")
public class HmzStorageRequestController extends BaseController
{
    @Autowired
    private IHmzStorageRequestService hmzStorageRequestService;
    @Autowired
    private IHmzStorageRequestDetailService hmzStorageRequestDetailService;
    @Autowired
    private IHmzProductService hmzProductService;


    /**
     * 查询用于存储入库申请信息的列表
     */
    @PreAuthorize("@ss.hasPermi('system:storagerequest:list')")
    @GetMapping("/list")
    public TableDataInfo list(HmzStorageRequest hmzStorageRequest)
    {
        startPage();
        List<HmzStorageRequest> list = hmzStorageRequestService.selectHmzStorageRequestList(hmzStorageRequest);
        return getDataTable(list);
    }

    /**
     * 进入系统之前初始化已入库数量
     * @param
     * @return
     */
    @GetMapping("/listNum")
    public void listNum()
    {
        HmzStorageRequest hmzStorageRequest = new HmzStorageRequest();
        List<HmzStorageRequest> list = hmzStorageRequestService.selectHmzStorageRequestList(hmzStorageRequest);
        if(list !=null  && list.size() > 0){
            for (HmzStorageRequest storageRequest : list) {
                if(!storageRequest.getTotalQuantity().equals(storageRequest.getReceivedQuantity())){
                    String storageOrderNumber = storageRequest.getStorageOrderNumber();
                    HmzStorageRequestDetail hmzStorageRequestDetail = new HmzStorageRequestDetail();
                    hmzStorageRequestDetail.setStorageOrderNumber(storageOrderNumber);
                    hmzStorageRequestDetail.setStorageStatus("在库");
                    List<HmzStorageRequestDetail> hmzStorageRequestDetails = hmzStorageRequestDetailService.selectHmzStorageRequestDetailList(hmzStorageRequestDetail);
                    storageRequest.setReceivedQuantity((long) hmzStorageRequestDetails.size());
                    hmzStorageRequestService.updateHmzStorageRequest(storageRequest);
                }

            }
        }

    }

    /**
     * 导出用于存储入库申请信息的列表
     */
    @PreAuthorize("@ss.hasPermi('system:storagerequest:export')")
    @Log(title = "用于存储入库申请信息的", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HmzStorageRequest hmzStorageRequest)
    {
        List<HmzStorageRequest> list = hmzStorageRequestService.selectHmzStorageRequestList(hmzStorageRequest);
        ExcelUtil<HmzStorageRequest> util = new ExcelUtil<HmzStorageRequest>(HmzStorageRequest.class);
        util.exportExcel(response, list, "用于存储入库申请信息的数据");
    }

    /**
     * 获取用于存储入库申请信息的详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:storagerequest:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(hmzStorageRequestService.selectHmzStorageRequestById(id));
    }

    /**
     * 新增用于存储入库申请信息的
     */
    @PreAuthorize("@ss.hasPermi('system:storagerequest:add')")
    @Log(title = "用于存储入库申请信息的", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HmzStorageRequest hmzStorageRequest)
    {
        return toAjax(hmzStorageRequestService.insertHmzStorageRequest(hmzStorageRequest));
    }

    /**
     * 手持机入库
     * @param infoListS
     * @return
     */
    @RequestMapping("/updateStatus")
    public AjaxResult updateStatus(String infoListS)
    {
        int i =0;
//        System.err.println(hmzStorageRequest);
        HmzStorageRequest hmzStorageRequest = JSON.parseObject(infoListS, HmzStorageRequest.class);
        //获取仓库id
        Long warehouseId = hmzStorageRequest.getWarehouseId();
        List<HmzProduct> infoListS1 = hmzStorageRequest.getInfoListS();
Long num =0L;
        for (HmzProduct hmzProduct : infoListS1) {
            //更新入库明细表状态
            HmzStorageRequestDetail hmzStorageRequestDetail = new HmzStorageRequestDetail();
            hmzStorageRequestDetail.setProductId(hmzProduct.getId());
            hmzStorageRequestDetail.setStorageStatus(hmzProduct.getProductStatus());
            hmzStorageRequestDetail.setStorageOrderNumber(hmzStorageRequest.getStorageOrderNumber());
            hmzStorageRequestDetailService.updateHmzStorageRequestDetail(hmzStorageRequestDetail);
            hmzProduct.setWarehouseId(warehouseId);
            //更新产品表状态
             i = hmzProductService.updateHmzProduct(hmzProduct);
             if("在库".equals(hmzProduct.getProductStatus())){
                 num = num+1L;
             }
        }
        hmzStorageRequest.setReceivedQuantity(num);
        hmzStorageRequestService.updateHmzStorageRequest(hmzStorageRequest);
        return toAjax(i);
    }

    /**
     * 修改用于存储入库申请信息的
     */
    @PreAuthorize("@ss.hasPermi('system:storagerequest:edit')")
    @Log(title = "用于存储入库申请信息的", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HmzStorageRequest hmzStorageRequest)
    {
        return toAjax(hmzStorageRequestService.updateHmzStorageRequest(hmzStorageRequest));
    }

    /**
     * 删除用于存储入库申请信息的
     */
    @PreAuthorize("@ss.hasPermi('system:storagerequest:remove')")
    @Log(title = "用于存储入库申请信息的", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(hmzStorageRequestService.deleteHmzStorageRequestByIds(ids));
    }
}
